﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Brain.Skinning;
using Microsoft.Xna.Framework.Input;
using Brain.Ballistics3D;

namespace Brain.FPS
{
    public class GamerWeaponManager : IGamerWeaponManager
    {
        public IGamerController Controller { get; set; }

        public List<Weapon> Weapons { get; set; }

        public int CurrentWeaponIndex = 0;
        public Weapon CurrentWeapon;

        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        private bool stickWeaponToCharacterHand = false;
        public bool StickWeaponToCharacterHand
        {
            get { return StickWeaponToCharacterHand; }
            set
            {
            }
        }

        public GamerWeaponManager(IGamerController con)
        {
            this.ShouldUpdate = true;
            this.ShouldDraw = true;

            this.Controller = con;
            Weapons = new List<Weapon>();

            Weapon weap = new Weapon(0);
            Weapons.Add(weap);
        }


        public void Update()
        {
        }

        public void Shoot()
        {
            if (CurrentWeapon != null)
            {
                if (CurrentWeapon.Shoot())
                {
                    BulletManager3D.Instance.AddBullet(CurrentWeapon.WorldObject.Position
                        + Vector3.Transform(CurrentWeapon.Info.ShotPosition, Controller.Camera.RotationMatrix), Controller.Camera.RotationMatrix.Forward,
                        CurrentWeapon.Info.BulletSpeed, OnHit);
                }
            }
        }


        public virtual void OnHit(Bullet3D bullet)
        {
        }

        private Vector3 CurrentPosition;
        public void Draw()
        {
            if (stickWeaponToCharacterHand)
            {
            }
            else
            {
                for (int i = 0; i < Weapons.Count; i++)
                {
                    Weapon weapon = Weapons[i];
                    if (i == CurrentWeaponIndex)
                    {
                        CurrentWeapon = weapon;

                        CharacterInfo character = this.Controller.CharInfo;

                        if (Controller.IsScoping)
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, weapon.Info.ScopePosition, 5);
                        }
                        else
                        {
                            CurrentPosition = AnimationUtil.SmoothAnimate(CurrentPosition, character.WeaponPosition, 5);
                        }

                        weapon.WorldObject.Position = this.Controller.Camera.Position + Vector3.Transform(CurrentPosition, this.Controller.Camera.RotationMatrix);
                        weapon.WorldObject.RotationRadians = this.Controller.Camera.RotationRadians;
                    }
                    weapon.Update();
                    weapon.Draw();
                }
            }
        }

    }
}
